package com.rundatop.wx.service.impl;

import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.github.pagehelper.PageHelper;
import com.rundatop.core.util.DateTimeUtil;
import com.rundatop.dao.mapper.SmsMapper;
import com.rundatop.dao.model.Sms;
import com.rundatop.dao.model.User;
import com.rundatop.wx.dto.Page;
import com.rundatop.wx.service.SmsService;
import com.rundatop.wx.util.AliyunSmsUtil;
import com.rundatop.wx.util.SmsUtil;

import me.chanjar.weixin.common.util.DataUtils;
import tk.mybatis.mapper.entity.Example;

/**
* 通用 Mapper 代码生成器
*
* @author mapper-generator
*/
@Service
public class SmsServiceImpl extends BaseService<Sms> implements SmsService{
	
	@Resource
    private SmsMapper smsMapper;

    @Override
    public int add(Sms entity, User loginUser) {

        //if (isBlank(entity.getCode())) {
        //    throw new BusinessException("代码不能为空");
        //}

       return smsMapper.insertSelective(entity);
    }

    @Override
    public int edit(Sms entity, User loginUser) {

        //if (isBlank(entity.getId())) {
        //    throw new BusinessException("Id不能为空");
        //}

        return smsMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public int deleteById(Serializable id, User loginUser) {
       return smsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Sms> getList(Sms condition, Page page, User loginUser) {
        Example ex = new Example(Sms.class);

        Example.Criteria c = ex.createCriteria();

        //if (isNotBlank(condition.getCode())) {
        //    c.andEqualTo("code", condition.getCode());
        //}

        //if (isNotBlank(condition.getName())) {
        //    c.andLike("name", fullLike(condition.getName()));
        //}

        //ex.orderBy("code").asc();

        if (page != null) {
            PageHelper.startPage(page.getPage(), page.getRows());
        }

        List<Sms> list = smsMapper.selectByExample(ex);
        return list;
    }

    @Override
    public Sms getById(Serializable id) {
        return smsMapper.selectByPrimaryKey(id);
    }

	@Override
	public int getSms(String phone) {
		String date = DateTimeUtil.getPreTime(DateTimeUtil.dateFormat(new Date()), "-30");
		Sms s = smsMapper.selectOneByPhoneAndCodeAndF1AndYouxiaoqi(phone,0,date);
		int code = 0;
		if (s!= null) {
			code = s.getCode();
		} else {
			code = (int)((Math.random()*9+1)*100000);
			Sms sms = new Sms();
			sms.setId(getUUID());
			sms.setCreatetime(new Date());
			sms.setPhone(phone);
			sms.setCode(code);
			sms.setF1("0");
			smsMapper.insert(sms);
		}
		 String phoneNumbers = phone;
        String signName = "手机虾";
        String templateCode = "SMS_489785208";
        String templateParam = "{\"code\":\""+code+"\"}";
		SendSmsResponse response = AliyunSmsUtil.sendSms(phoneNumbers, signName, templateCode, templateParam);
        if(response != null && "OK".equals(response.getCode())) {
            System.out.println("短信发送成功!");
        } else {
            System.out.println("短信发送失败!");
        }
//		SmsUtil.getSms(code, phone);
		return code;
	}
	
	@Override
	public String verifyCode(String phone, int code) {
		String date = DateTimeUtil.getPreTime(DateTimeUtil.dateFormat(new Date()), "-30");
		System.out.println(date);
		Sms s = smsMapper.selectOneByPhoneAndCodeAndF1AndYouxiaoqi(phone,code,date);
		if (s != null) {
			//判断时间
			s.setF1("1");
			smsMapper.updateByPrimaryKey(s);
			return "200";
		}
		return "500";
	}
}




